import { dataSource } from '../db/index.js'
import ShopGoldEntity from '../entity/shopGold.js'
import BankGoldEntity from '../entity/bankGold.js'
import dayjs from 'dayjs'
import Big from 'big.js'

import { shopDetailList, bankType } from './config.js'

const shopGoldRepository = dataSource.getRepository(ShopGoldEntity)
const bankGoldRepository = dataSource.getRepository(BankGoldEntity)


export const getShopGold = async (req, res) => {
  let { type } = req.body

  let query = {}

  if (type && type.trim()) {
    type = type.trim()
    const validType = shopDetailList.find(p => p.code === type)

    if (validType) {
      query.type = type
    }
  }

  try {
    let result = await shopGoldRepository.createQueryBuilder('shop')
      .where(query)
      .getManyAndCount()


    let list = []
    let total = 0

    if (result && result.length) {
      list = result[0]

      list.forEach(p => {
        let typeText = shopDetailList.find(item => item.code === p.type).name
        p.typeText = typeText

        p.createTime = dayjs(p.createTime).format('YYYY-MM-DD HH:mm:ss')
        p.updateTime = dayjs(p.updateTime).format('YYYY-MM-DD HH:mm:ss')

        p.price = Big(p.price).toNumber()
      })
      total = result[1]
    }

    res.json({
      code: 1000,
      data: {
        list: result[0],
        total: result[1]
      },
      message: '操作成功'
    })
  } catch (error) {
    console.log(error);
    res.json({
      code: -1,
      data: null,
      message: '操作失败' + error.message
    })
  }


}

export const getBankGold = async (req, res) => { }
